# CampBattleView2D Makefile

# 检测操作系统
ifeq ($(OS),Windows_NT)
    PYTHON = venv/Scripts/python
    PIP = venv/Scripts/pip
    ACTIVATE = venv/Scripts/activate
else
    PYTHON = venv/bin/python
    PIP = venv/bin/pip
    ACTIVATE = source venv/bin/activate
endif

# 默认目标
.PHONY: help
help:
	@echo "CampBattleView2D 开发工具"
	@echo "=========================="
	@echo "setup     - 配置开发环境"
	@echo "install   - 安装依赖"
	@echo "run       - 运行游戏"
	@echo "test      - 运行测试"
	@echo "format    - 格式化代码"
	@echo "lint      - 代码检查"
	@echo "type      - 类型检查"
	@echo "clean     - 清理临时文件"
	@echo "build     - 构建分发包"
	@echo "profile   - 性能分析"

# 环境配置
.PHONY: setup
setup:
	python setup_env.py

# 安装依赖
.PHONY: install
install:
	$(PIP) install --upgrade pip
	$(PIP) install -r requirements.txt

# 运行游戏
.PHONY: run
run:
	$(PYTHON) run.py

# 运行测试
.PHONY: test
test:
	$(PYTHON) -m pytest tests/ -v --cov=src/camp_battle

# 代码格式化
.PHONY: format
format:
	$(PYTHON) -m black src/ tests/ setup.py run.py
	$(PYTHON) -m isort src/ tests/ setup.py run.py

# 代码检查
.PHONY: lint
lint:
	$(PYTHON) -m flake8 src/ tests/
	$(PYTHON) -m black --check src/ tests/

# 类型检查
.PHONY: type
type:
	$(PYTHON) -m mypy src/camp_battle

# 清理临时文件
.PHONY: clean
clean:
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	rm -rf build/ dist/ .coverage htmlcov/ .pytest_cache/
	rm -rf logs/*.log

# 构建分发包
.PHONY: build
build:
	$(PYTHON) -m build

# 性能分析
.PHONY: profile
profile:
	$(PYTHON) -m cProfile -o profile.stats run.py
	$(PYTHON) -c "import pstats; p = pstats.Stats('profile.stats'); p.sort_stats('cumulative'); p.print_stats(20)"

# 开发模式安装
.PHONY: dev-install
dev-install:
	$(PIP) install -e .[dev,profiling,analysis]

# 生成需求文件
.PHONY: freeze
freeze:
	$(PIP) freeze > requirements-lock.txt

# 启动Jupyter
.PHONY: jupyter
jupyter:
	$(PYTHON) -m jupyter notebook

# 内存分析
.PHONY: memory
memory:
	$(PYTHON) -m memory_profiler run.py

# 代码覆盖率报告
.PHONY: coverage
coverage:
	$(PYTHON) -m pytest tests/ --cov=src/camp_battle --cov-report=html
	@echo "Coverage report generated in htmlcov/"

# 全面检查
.PHONY: check
check: lint type test
	@echo "✅ 所有检查通过"

# 发布准备
.PHONY: release-prep
release-prep: clean format check build
	@echo "🚀 发布准备完成"
